<template>
    <div  class="linkList"  @click.stop>
        <span style="color:black">链接</span>
        <input type="text" placeholder="链接文字,如果没有可以不填" class="linkeAddr" v-model="linkTitle">
        <input type="text" placeholder="链接地址" class="linkeAddr" v-model="linkAddr">
        <div class="footer">
            <button  @click.prevent.stop="handleClose">取消</button>
            <button  @click.prevent.stop="inserLink">确定</button>
        </div>
    </div>
</template>

<script>
import mixin from '../../mixins/index'
export default {
    props:['range'],
    mixins: [mixin],
    data(){
        return {
            linkTitle: '',
            linkAddr: ''
        }
    },
    methods: {
        inserLink(){
            // this.restoreRange();
            /**
             * 如果只希望创建链接而不创建文件,则直接执行
             * this.execCommand("CreateLink",this.linkAddr);
             * 否则插入a标签到指定的位置
             */
            if(this.linkTitle.trim() || this.linkAddr.trim()){
                var innerDom = this.range.extractContents();
                var link = document.createElement("a");
                link.setAttribute("href",this.linkAddr);
                link.innerHTML = this.linkTitle || this.linkAddr;
                link.append(innerDom);
                this.range.insertNode(link);
            }
            this.$emit('selectmenu')
            this.handleClose();
        },
        handleClose(){
            this.$parent.show = false;
            this.linkTitle = '';
            this.linkAddr = '';
        }
    }
}
</script>

<style scoped>
    .link-enter-active,.link-leave-active {
        transition: all .2s;
    }
    .link-enter,.link-leave-to {
        transform: scale(0.2);
        opacity: 0;
    }
    .linkList {
        width: 250px;
        background: white;
        padding: 5px;
        box-shadow: 0px 0px 1px 1px rgb(189, 189, 189);
        position: relative;
        left: -140px;
    }
    .linkeAddr {
        width: 100%;
        padding: 5px 2px;
        outline: none;
        box-sizing: border-box;
        border: none;
        border-bottom: 1px solid lightgray;
        margin-top: 10px;
        font-size: 12px;
    }
    .footer{
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-align: right;
    }
    .footer button {
        width: 50px;
        margin-right: 5px;
        outline: none;
        background: white;
        border: 1px solid gray;
        color: rgb(65, 63, 63);
        border-radius: 3px;
    }
</style>
